﻿using LCL.Network.Enthernet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LCL.NetworkDemo
{
    public partial class FormPushNet : Form
    {
        public FormPushNet()
        {
            InitializeComponent();
        }
        private void FormPushNet_Load(object sender, EventArgs e)
        {
            textBox15.Text = Guid.Empty.ToString();
            panel2.Enabled = false;
        }
        private NetPushClient pushClient;
        private int receiveCount = 0;
        private void button1_Click(object sender, EventArgs e)
        {
             int port;
            if (!int.TryParse( textBox2.Text, out port ))
            {
                MessageBox.Show( "端口号输入错误" );
                return;
            }

            if (string.IsNullOrEmpty( textBox3.Text ))
            {
                MessageBox.Show( "关键字不能为空" );
                return;
            }

            if(pushClient!=null)pushClient.ClosePush( );
            pushClient = new NetPushClient( textBox1.Text, port, textBox3.Text );
            Result create = pushClient.CreatePush( new Action<NetPushClient, string>( PushFromServer ) );
            if (create.Success)
            {
                button1.Enabled = false;
                button2.Enabled = true;
                MessageBox.Show( "成功" );
                panel2.Enabled = true;
            }
            else
            {
                MessageBox.Show( "失败：" + create.Message );
            }
        }
        private void PushFromServer(NetPushClient pushClient, string data)
        {
            if (IsHandleCreated) Invoke(new Action<string>(m =>
            {
                label8.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                receiveCount++;
                label9.Text = receiveCount.ToString();
                textBox4.Text = m;
            }), data);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //断开连接
            pushClient.ClosePush();
            panel2.Enabled = false;
            button1.Enabled = true;
            button2.Enabled = false;
        }

       
    }
}
